package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/">2038. 如果相邻两个颜色均相同则删除当前颜色</a>
 */
public class Solution2038 extends BaseSolution {

    public boolean winnerOfGame(String colors) {
        int ca = 0, cb = 0;
        char last = '.';
        int lc = 0;
        for (char c : colors.toCharArray()) {
            if (c == last) {
                lc++;
            } else {
                if (last == 'A') {
                    ca += Math.max(0, lc - 2);
                } else if (last == 'B') {
                    cb += Math.max(0, lc - 2);
                }
                last = c;
                lc = 1;
            }
        }
        if (last == 'A') {
            ca += Math.max(0, lc - 2);
        } else if (last == 'B') {
            cb += Math.max(0, lc - 2);
        }
        return ca > cb;
    }

    public static void main(String[] args) {
        Solution2038 solution = new Solution2038();
        System.out.println(solution.winnerOfGame("ABBBBBBBAAA"));
    }
}